package com.four.order.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.four.common.vo.ShoppingVo;
import com.four.order.dao.ShoppingDao;
import com.four.order.entity.OrShopping;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @Author: hxw
 * @Date: 2022/1/14 20:40
 */
@Configuration
public class RabbitMqConfig {

    @Autowired
    private ShoppingDao shoppingDao;

    /**
     * 消费者
     */
    @RabbitListener(queues = "order-cunsume")
    @RabbitHandler
    public void updateOrder(Message message, String msg, Channel channel) {
        ShoppingVo shoppingVo = JSON.parseObject(msg, new TypeReference<ShoppingVo>() {
        });
        try {
            OrShopping byId = shoppingDao.getById(shoppingVo.getOrId());
            Integer status = 0;
            if (status.equals(byId.getOrStatus())) {
                byId.setOrStatus(4);
                shoppingDao.updateByStatus(byId);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
